package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 创建Article对象,里面属性:title,content
 * 程序启动后,要求用户输入标题和正文,然后将数据保存到article表中
 */
public class Test3 {
    public static void main(String[] args) {
        Article article = InputUtil.getInputObject(new Article(),"发表文章");

        try (Connection connection = DBUtil.getConnection();){
            /*
                INSERT INTO article(title,content)
                VALUES(?,?)
             */
            String sql = "INSERT INTO article(title,content)" +
                         "VALUES(?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,article.getTitle());
            ps.setString(2,article.getContent());
            int num = ps.executeUpdate();
            if(num>0){
                System.out.println("文章发表成功");
            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}






